package com.zcx.controller;

import com.zcx.bean.FileModel;
import com.zcx.bean.ResultBus;
import com.zcx.service.FileService;
import com.zcx.util.TimeUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.UUID;


@RestController
@RequestMapping("file")
public class FileController {

    @Resource
    private FileService fileService;

    private static final String BASE_PATH = "E://zcx//";

    @PostMapping("/uploadFile")
    public ResultBus uploadFile(@RequestParam("file")MultipartFile file){
        ResultBus resultBus = new ResultBus();
        //获取uuid
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        //获取保存路径
        String path = BASE_PATH + TimeUtil.yyyyMMdd()+"//"+uuid+"."+file.getOriginalFilename().split("\\.")[1];
        //封装file有关信息
        FileModel fileModel1 = new FileModel();
        fileModel1.setSize(file.getSize());
        fileModel1.setName(file.getOriginalFilename().split("\\.")[0]);
        fileModel1.setSaveTime(TimeUtil.yyyy_MM_dd_HH_mm_ss());
        fileModel1.setType(file.getOriginalFilename().split("\\.")[1]);
        fileModel1.setUrl(path);
        fileModel1.setUUID(uuid);
        fileModel1.setState(1);
        boolean flag = fileService.saveFileToPath(file, path);
        if (!flag){
            resultBus.setResult_msg("上传失败");
            resultBus.setResult_num(500);
            resultBus.setData("Error");
            return resultBus;
        }
        boolean result = fileService.saveFileToMysql(fileModel1);
        if (!result){
            fileService.deleteFileByPath(path);
            resultBus.setResult_msg("上传失败");
            resultBus.setResult_num(500);
            resultBus.setData("Error");
            return resultBus;
        }
        resultBus.setData(uuid);
        resultBus.setResult_msg("上传成功");
        return resultBus;
    }
    @GetMapping("/downloadFile")
    public void download(@RequestParam("uuid")String uuid, HttpServletResponse response){
        // 开始下载文件
        String path = fileService.getPathByUUID(uuid);
        try {
            // path是指欲下载的文件的路径。
            File file = new File(path);
            // 取得文件名。
            String filename = file.getName();
            // 取得文件的后缀名。
            String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
            // 以流的形式下载文件。
            InputStream fis = new BufferedInputStream( new FileInputStream(path));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header //ISO-8859-1可以显示中文的文件名
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes(),"ISO-8859-1"));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    @GetMapping("/getInfoByUUID")
    public ResultBus getInfoByUUID(@RequestParam("uuid")String uuid){
        ResultBus resultBus = new ResultBus();
        FileModel fileModel = fileService.getInfoByUUID(uuid);
        if (fileModel == null){
            resultBus.setResult_msg("获取失败");
            resultBus.setResult_num(500);
            resultBus.setData(null);
        }
        resultBus.setData(fileModel);
        resultBus.setResult_msg("获取成功");
        resultBus.setResult_num(200);
        return resultBus;
    }
}

